/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月30日         Root.Lu
 */

package com.coocaa.launcher.framework.launcherhost;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.coocaa.x.framework.utils.LiteHandlerThread;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperList;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.tianci.appstore.R;

public class WallPaper extends BaseView {
    private CoocaaDraweeView wallpaper = null;
    private LiteHandlerThread ht = null;
    private String file = null;
    private BitmapDrawable drawable = null;

    // private ImageView liveblur = null;

    public WallPaper(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        // setBackgroundColor(Color.WHITE);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        wallpaper = new CoocaaDraweeView(context);
        wallpaper.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
        wallpaper.setLayoutParams(params);
        //
        // liveblur = new ImageView(context);
        // liveblur.setLayoutParams(params);
        addView(wallpaper);
        // addView(liveblur);
    }

    public void setWallpaperItem(final RP_WallpaperList.WallPaper_Item item) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(
                                Uri.parse("res://" + mContext.getPackageName() + "/" + R.drawable.bg_1))
                                .setResizeOptions(new ResizeOptions(1280, 720))
                                .build();
                        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                                .setOldController(wallpaper.getController())
                                .setImageRequest(request)
                                .build();
                        wallpaper.setController(controller);
                    }
                };
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(item.bigUrl))
                        .setResizeOptions(new ResizeOptions(1280, 720))
                        .build();
                DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                        .setControllerListener(controllerListener)
                        .setOldController(wallpaper.getController())
                        .setImageRequest(request)
                        .setLowResImageRequest(ImageRequest.fromUri(Uri.parse(item.smallUrl)))
                        .build();
                wallpaper.setController(controller);
            }
        });

    }

    @Override
    public void show() {
        wallpaper.setVisibility(View.VISIBLE);
    }

    @Override
    public void hide() {
        wallpaper.setVisibility(View.GONE);
    }
}
